package com.tc168.cp306.b06.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.maning.mndialoglibrary.MProgressDialog;
import com.tc168.cp306.b06.MyApplication;
import com.tc168.cp306.b06.R;
import com.tc168.cp306.b06.Utils.ToastUtil;
import com.tc168.cp306.b06.adaptre.Answer_itemAdapter;
import com.tc168.cp306.b06.bean.Topticbean;
import com.tc168.cp306.b06.gen.TopticbeanDao;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SouActivity extends Activity {

    @BindView(R.id.back_ig)
    ImageView backIg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rv)
    RecyclerView anRv;
    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.sou)
    Button sou;

    private TopticbeanDao topticbeanDao;
    private List<Topticbean> list;
    private Answer_itemAdapter answer_itemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sou);
        ButterKnife.bind(this);
        topticbeanDao = MyApplication.getmDaoSession().getTopticbeanDao();
        list = new ArrayList<>();
        answer_itemAdapter = new Answer_itemAdapter(SouActivity.this, list);

        anRv.setAdapter(answer_itemAdapter);
        anRv.setLayoutManager(new LinearLayoutManager(this));

        answer_itemAdapter.setOnItemClickListener(new Answer_itemAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                Intent intent = new Intent(SouActivity.this, MatterActivity.class);
                intent.putExtra("num", postion);
                intent.putExtra("type", list.get(postion).getToptictype());
                startActivity(intent);
            }
        });


    }


    @OnClick({R.id.back_ig, R.id.sou})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_ig:
                finish();
                break;
            case R.id.sou:

                MProgressDialog.showProgress(this);


                new Handler().postDelayed(new Runnable() {

                    @Override

                    public void run() {
                        MProgressDialog.dismissProgress();
                    }
                }, 1000);

                list.clear();
                List<Topticbean> listaaa = topticbeanDao.queryBuilder().where(TopticbeanDao.Properties.Title.eq(et.getText().toString())).list();
                list.addAll(listaaa);
                answer_itemAdapter.notifyDataSetChanged();
                if (listaaa==null||listaaa.size()==0){
                    ToastUtil.showLong(this,"没有搜索到相关信息");
                }
                break;
        }
    }
}
